<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :rules="rules" size="small" :inline="true">
      <!-- <el-form-item label="房间id" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入房间id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="房间号" prop="roomNo">
        <el-input
          v-model="queryParams.roomNo"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间状态" prop="statusId">
        <el-select v-model="choose" placeholder="房间状态" clearable>
          <el-option label="空房" value="空房" @click.native="handleStatus" />
          <el-option label="已预定" value="已预定" @click.native="handleStatus" />
          <el-option label="已入住" value="已入住" @click.native="handleStatus" />
          <el-option label="待清理" value="待清理" @click.native="handleStatus" />
          <!-- <el-option
            v-for="item in typeOptions"
            :key="item.roomId"
            :label="item.statusId"
            :value="item.roomId"
            @keyup.enter.native="handleQuery"
          /> -->
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="房间等级" prop="roomtypeId">
        <el-select v-model="queryParams.roomTypeId" placeholder="房间等级" clearable>
          <el-option
            v-for="item in typeOptions"
            :key="item.roomTypeId"
            :label="item.roomGrade"
            :value="item.roomTypeId"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="房间等级" prop="roomTypeId">
        <el-select v-model="grade" placeholder="房间等级" clearable>
          <el-option label="商务房" value="商务房" @click.native="handleGrade" />
          <el-option label="双人大床房" value="双人大床房" @click.native="handleGrade" />
          <el-option label="总统套房" value="总统套房" @click.native="handleGrade" />
          <!-- <el-option
            v-for="item in typeOptions"
            :key="item.hotelRoomType.roomTypeId"
            :label="item.hotelRoomType.roomGrade"
            :value="item.hotelRoomType.roomTypeId"
            @keyup.enter.native="handleQuery"
          /> -->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:role:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-tag
          v-for="item in items"
          :key="item.label"
          :type="item.type"
          effect="dark"
        >
          {{ item.label }}
        </el-tag>
        <!-- <el-tag>空房</el-tag>
        <el-tag type="success">已预定</el-tag>
        <el-tag type="warning">已入住</el-tag>
        <el-tag type="danger">待清洁</el-tag> -->
      </el-col>
    </el-row>

    <!-- <div :data="typeList">
      <el-row>
        <div class="roomS">
          <el-col v-for="(room) in typeList" :key="room.roomId">
            <div v-if="room.roomStatus==空房">
              <div class="div1">
              </div>
            </div>
          </el-col>
        </div>
      </el-row>
    </div> -->
    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="房间id" align="center" prop="roomId" /> -->
      <el-table-column label="房间号" align="center" prop="roomNo" />
      <el-table-column label="房间类型" align="center" prop="hotelRoomType.roomGrade" />
      <el-table-column label="房间状态" align="center" prop="statusId">
        <!-- 使用tag改变颜色 -->
        <template slot-scope="scope">
          <div v-if="scope.row.statusId == 2">
            <el-tag
              type="success"
              effect="dark"
              disable-transitions
            >{{ "空房" }}</el-tag>
          </div>
          <div v-if="scope.row.statusId == 4">
            <el-tag
              type=""
              effect="dark"
              disable-transitions
            >{{ "已预定" }}</el-tag>
          </div>
          <div v-if="scope.row.statusId == 1">
            <el-tag
              type="danger"
              effect="dark"
              disable-transitions
            >{{ "已入住" }}</el-tag>
          </div>
          <div v-if="scope.row.statusId == 3">
            <el-tag
              type="warning"
              effect="dark"
              disable-transitions
            >{{ "待清理" }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:post:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:post:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      align="center"
      :current-page="pageNum"
      :page-sizes="[5, 10, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="房间类型id" prop="roomTypeId">
          <el-input v-model="form.roomTypeId" placeholder="请输入房间类型id" />
        </el-form-item> -->
        <!-- <el-form-item label="房间id" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房间Id" />
        </el-form-item> -->
        <el-form-item label="房间号" prop="roomNo">
          <el-input v-model="form.roomNo" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="房间类型" prop="typeId">
          <el-select v-model="form.roomType" placeholder="房间等级" clearable @change="selectroomtype">
            <el-option label="商务房" value="1" />
            <el-option label="双人大床房" value="2" />
            <el-option label="总统套房" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="房间状态" prop="statusId">
          <!-- <el-input v-model="form.roomStatus" placeholder="请输入房间状态" /> -->
          <el-select v-model="form.status" placeholder="请选择房间状态" clearable @change="selectstatus">
            <el-option label="空房" value="2" />
            <el-option label="已预定" value="4" />
            <el-option label="已入住" value="1" />
            <el-option label="待清理" value="3" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="房间等级" prop="roomGrade">
          <el-input v-model="form.roomGrade" placeholder="请输入房间等级" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listRoomByPage, listType, getType, addType, updateType, delType } from '@/api/manage/room'
// import { status } from 'nprogress'
export default {
  components: {},
  props: [],
  data() {
    return {
      activeName: 'first',
      // 房间等级
      typeOptions: undefined,
      // 遮罩层
      loading: true,
      // 字典表格数据
      typeList: [],
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        roomNo: undefined,
        roomTypeId: undefined,
        statusId: undefined,
        roomType: undefined,
        status: undefined
      },
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      choose: '',
      grade: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomId: undefined,
        roomNo: undefined,
        statusId: undefined,
        roomTypeId: undefined,
        roomGrade: undefined,
        hotelRoomType: undefined
      },
      items: [
        { type: 'success', label: '空房' },
        { type: '', label: '已预定' },
        { type: 'danger', label: '已入住' },
        { type: 'warning', label: '待清理' }
      ],
      rules: {
        roomNo: [{
          required: true,
          message: '房间号不能为空',
          trigger: 'blur'
        }],
        roomId: [{
          required: true,
          message: '房间id不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.fetchData()
    listType().then((response) => {
      this.typeOptions = response.data
    })
  },
  mounted() {},
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    /** 查询房间列表 */
    fetchData() {
      this.loading = true
      listRoomByPage(this.queryParams).then((response) => {
        this.typeList = response.data.list
        this.total = response.data.total
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        this.loading = false
      })
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.fetchData()
    },
    // 表单重置
    resetForm() {
      this.form = {
        roomId: undefined,
        roomNo: undefined,
        statusId: undefined,
        typeId: undefined
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        roomId: undefined,
        roomNo: undefined,
        statusId: undefined,
        roomTypeId: undefined
      }
      this.choose = undefined
      this.grade = undefined
      this.fetchData()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.fetchData()
      this.$message.success('查询成功')
    },
    /** 下拉框选择操作 */
    handleStatus() {
      switch (this.choose) {
        case '已入住':
          this.queryParams.statusId = 1
          this.fetchData()
          break
        case '空房':
          this.queryParams.statusId = 2
          this.fetchData()
          break
        case '待清理':
          this.queryParams.statusId = 3
          this.fetchData()
          break
        case '已预定':
          this.queryParams.statusId = 4
          this.fetchData()
          break
      }
    },
    handleGrade() {
      switch (this.grade) {
        case '商务房':
          this.queryParams.roomTypeId = 1
          this.fetchData()
          break
        case '双人大床房':
          this.queryParams.roomTypeId = 2
          this.fetchData()
          break
        case '总统套房':
          this.queryParams.roomTypeId = 3
          this.fetchData()
          break
      }
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.resetForm()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm()
      this.open = true
      this.title = '新增房间信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm()
      getType(row.roomId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改房间信息'
      })
    },
    // 修改房间类型和房间状态
    selectroomtype(value) {
      switch (value) {
        case '1':
          this.form.roomType = '商务房'
          this.form.roomTypeId = '1'
          break
        case '2':
          this.form.roomType = '双人大床房'
          this.form.roomTypeId = '2'
          break
        case '3':
          this.form.roomType = '总统套房'
          this.form.roomTypeId = '3'
          break
      }
    },
    //
    selectstatus(value) {
      switch (value) {
        case '1':
          this.form.status = '已入住'
          this.form.statusId = '1'
          break
        case '2':
          this.form.status = '空房'
          this.form.statusId = '2'
          break
        case '3':
          this.form.status = '待清理'
          this.form.statusId = '3'
          break
        case '4':
          this.form.status = '已预定'
          this.form.statusId = '4'
          break
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '修改房间信息') {
            updateType(this.form).then((response) => {
              this.open = false
              this.title = ''
              this.fetchData()
              // this.$message.success(response.data) // 后端返回消息字符串
              this.$message.success('修改成功')
            })
          } else {
            addType(this.form).then((response) => {
              this.open = false
              this.title = ''
              this.fetchData()
              // this.$message.success(response.data)
              this.$message.success('添加成功')
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除房间号为"' + row.roomNo + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delType(row.roomId)
        })
        .then(() => {
          this.fetchData()
          this.$message.success('删除成功')
        })
        .catch(() => {})
    }
  }
}

</script>
<style>
.el-tag {
  margin-right: 10px;
}
.el-button{
  margin-bottom: 15px;
}
/* .div1 {
            width: 110px;
            height: 70px;
            border: 1px solid #1cc09f;
            border-radius: 5px;
            background: #1cc09f;
            float: left;
            margin: 2px;
            text-align: center;
            color: white;
            cursor: pointer;
        }
.div2 {
            width: 110px;
            height: 70px;
            border: 1px solid #1cc09f;
            border-radius: 5px;
            background: #ec7e7e;
            float: left;
            margin: 2px;
            text-align: center;
            color: white;
            cursor: pointer;
        }

.div3 {
            width: 110px;
            height: 70px;
            border: 1px solid #1cc09f;
            border-radius: 5px;
            background: #f3a633;
            float: left;
            margin: 2px;
            text-align: center;
            color: white;
            cursor: pointer;
        } */

</style>
